//
//  TBInputViewController.swift
//  新浪微博
//
//  Created by bin on 16/2/4.
//  Copyright © 2016年 XMG. All rights reserved.
//

import UIKit

@objc protocol TBInputViewDelegte: NSObjectProtocol {
    
    optional func inputViewCellDidCliked(model: TBEmoticonModel);
    
}

class TBInputViewController: UIViewController {

    @IBOutlet weak var myCollectionView: UICollectionView!
    weak var delegate: TBInputViewDelegte?
    override func viewDidLoad() {
        super.viewDidLoad()
        myCollectionView.registerClass(TBInputCollectionCell.self, forCellWithReuseIdentifier: "InputCollectionCell")
        
        
    }
    //MARK: - 内部控制方法
    
    @IBAction func scrollToPackage(sender: UIBarButtonItem) {
        let indexPath = NSIndexPath(forItem: 0, inSection: sender.tag)
        myCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
    }
    
    
    //MARK: - 懒加载
    
    lazy var packageModels: [TBPackagesModel] = TBPackagesModel.loadAllPackage()

}

class TBInputCollectionCell: UICollectionViewCell {
    
    var emoticon: TBEmoticonModel?{
        didSet{
            iconBtn.setTitle("\(emoticon!.codeStr ?? "")", forState: UIControlState.Normal)
            iconBtn.setImage(UIImage(contentsOfFile: emoticon!.pngPath ?? ""), forState: UIControlState.Normal)
            iconBtn.setImage(UIImage(named: ""), forState: UIControlState.Highlighted)
            if emoticon!.isRemoveBtn{
                iconBtn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
                iconBtn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupSubviews()
    }
    
    //MARK: - 内部控制方法
    
    private func setupSubviews(){
        contentView.addSubview(iconBtn)
        // align iconBtn from the left and right
        iconBtn.frame = CGRectInset(contentView.bounds, 3, 3)

    }
    
    //MARK: - 外部控制方法
    
    //MARK: - 懒加载
    lazy var iconBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        btn.userInteractionEnabled = false
        return btn
    }()
    
}

class TBFlowLayout: UICollectionViewFlowLayout{
    
    override func prepareLayout() {
        super.prepareLayout()
        let itemWidth = ScreenW / 7.0
        itemSize = CGSizeMake(itemWidth, itemWidth)
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.pagingEnabled = true
        let inset = ((collectionView?.bounds.height)! - 3 * itemWidth) * 0.5
        collectionView?.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0)
    }
    
}

extension TBInputViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packageModels.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return packageModels[section].emoticons!.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InputCollectionCell", forIndexPath: indexPath) as! TBInputCollectionCell
        cell.emoticon = packageModels[indexPath.section].emoticons![indexPath.item]

        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let emoticon = packageModels[indexPath.section].emoticons![indexPath.item]

        delegate?.inputViewCellDidCliked?(emoticon)
        
    }
}



